import ObjectPool from "./ObjectPool";
import TSMap from "../struct/TSMap";

export default class PoolManager {


    private static ins: PoolManager

    static instance() {
        if (!this.ins) {
            this.ins = new PoolManager();
        }
        return this.ins;
    }
    
    private map: TSMap<string, ObjectPool<any>> = new TSMap();

    get(key: any, func: () => any) {
        if (!this.map.has(key)) {
            this.map.set(key, new ObjectPool(key))
        }
        return this.map.get(key).get(func)
    }

    put(key: any, obj: any) {
        if (this.map.has(key)) {
            this.map.get(key).put(obj)
        } else {
        }
    }

    size(key: string) {
        if (this.map.has(key)) {
            return this.map.get(key).size()
        }
        return 0;
    }


    destroy() {
        this.map.forEach((key, value) => {
            value.destroy();
        })
        this.map.clear();
    }


}